<template>
  <a-modal v-model="visible" :mask-closable="false" width="75%" title="查看" :dialog-style="{ top: '20px' }" :footer="null">
    <div v-if="visible" v-permission="['stock:sc-transfer:query']" v-loading="loading">
      <j-border>
        <j-form>
          <j-form-item label="转出仓库" required>
            {{ formData.sourceScName }}
          </j-form-item>
          <j-form-item label="转入仓库" required>
            {{ formData.targetScName }}
          </j-form-item>
          <j-form-item label="备注" :span="24">
            <a-textarea v-model.trim="formData.description" read-only />
          </j-form-item>
          <j-form-item label="状态">
            <span v-if="$enums.SC_TRANSFER_ORDER_STATUS.APPROVE_PASS.equalsCode(formData.status)" style="color: #52C41A;">{{ $enums.SC_TRANSFER_ORDER_STATUS.getDesc(formData.status) }}</span>
            <span v-else-if="$enums.SC_TRANSFER_ORDER_STATUS.APPROVE_REFUSE.equalsCode(formData.status)" style="color: #F5222D;">{{ $enums.SC_TRANSFER_ORDER_STATUS.getDesc(formData.status) }}</span>
            <span v-else style="color: #303133;">{{ $enums.SC_TRANSFER_ORDER_STATUS.getDesc(formData.status) }}</span>
          </j-form-item>
          <j-form-item label="拒绝理由" :span="16" :content-nest="false">
            <a-input v-if="$enums.SC_TRANSFER_ORDER_STATUS.APPROVE_REFUSE.equalsCode(formData.status)" v-model="formData.refuseReason" read-only />
          </j-form-item>
          <j-form-item label="操作人">
            <span>{{ formData.updateBy }}</span>
          </j-form-item>
          <j-form-item label="操作时间" :span="16">
            <span>{{ formData.updateTime }}</span>
          </j-form-item>
          <j-form-item v-if="$enums.SC_TRANSFER_ORDER_STATUS.APPROVE_PASS.equalsCode(formData.status) || $enums.SC_TRANSFER_ORDER_STATUS.APPROVE_REFUSE.equalsCode(formData.status)" label="审核人">
            <span>{{ formData.approveBy }}</span>
          </j-form-item>
          <j-form-item v-if="$enums.SC_TRANSFER_ORDER_STATUS.APPROVE_PASS.equalsCode(formData.status) || $enums.SC_TRANSFER_ORDER_STATUS.APPROVE_REFUSE.equalsCode(formData.status)" label="审核时间" :span="16">
            <span>{{ formData.approveTime }}</span>
          </j-form-item>
        </j-form>
      </j-border>

      <!-- 数据列表 -->
      <vxe-grid
        ref="grid"
        resizable
        show-overflow
        highlight-hover-row
        keep-source
        row-id="id"
        height="500"
        :data="tableData"
        :columns="tableColumn"
      >
        <!-- 已收货数量 列自定义内容 -->
        <template v-slot:receiveNum_default="{ row }">
          <span v-if="$utils.gt(row.receiveNum, 0)"><a @click="detailReceive(row.id)">{{ row.receiveNum }}</a></span>
          <span v-else>{{ row.receiveNum }}</span>
        </template>
      </vxe-grid>

      <order-time-line :id="id" />

      <j-border title="合计">
        <j-form label-width="140px">
          <j-form-item label="调拨数量" :span="6">
            <a-input v-model="formData.totalNum" class="number-input" read-only />
          </j-form-item>
          <j-form-item v-if="$enums.SC_TRANSFER_ORDER_STATUS.APPROVE_PASS.equalsCode(formData.status) || $enums.SC_TRANSFER_ORDER_STATUS.PART_RECEIVED.equalsCode(formData.status) || $enums.SC_TRANSFER_ORDER_STATUS.RECEIVED.equalsCode(formData.status)" label="调拨成本金额" :span="6">
            <a-input v-model="formData.totalAmount" class="number-input" read-only />
          </j-form-item>
        </j-form>
      </j-border>
    </div>

    <detail-receive ref="detailReceiveDialog" :order-id="id" :detail-id="detailId" />
  </a-modal>
</template>
<script>

import DetailReceive from './detail-receive'
export default {
  components: {
    DetailReceive
  },
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 是否可见
      visible: false,
      // 是否显示加载框
      loading: false,
      // 表单数据
      formData: {},
      detailId: '',
      // 列表数据配置
      tableColumn: [
        { type: 'seq', width: 40 },
        { field: 'productCode', title: '商品编号', width: 120 },
        { field: 'productName', title: '商品名称', width: 260 },
        { field: 'skuCode', title: '商品SKU编号', width: 120 },
        { field: 'externalCode', title: '商品外部编号', width: 120 },
        { field: 'unit', title: '单位', width: 80 },
        { field: 'spec', title: '规格', width: 80 },
        { field: 'categoryName', title: '商品类目', width: 120 },
        { field: 'brandName', title: '商品品牌', width: 120 },
        { field: 'transferNum', title: '调拨数量', width: 120, align: 'right' },
        { field: 'receiveNum', title: '已收货数量', width: 120, align: 'right', slots: { default: 'receiveNum_default' }},
        { field: 'description', title: '备注', width: 200 }
      ],
      tableData: []
    }
  },
  computed: {
  },
  created() {
    // 初始化表单数据
    this.initFormData()
  },
  methods: {
    // 打开对话框 由父页面触发
    openDialog() {
      // 初始化表单数据
      this.initFormData()
      this.visible = true
      this.$nextTick(() => this.open())
    },
    // 关闭对话框
    closeDialog() {
      this.visible = false
      this.$emit('close')
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {
        sourceScName: '',
        targetScName: '',
        description: '',
        updateBy: '',
        updateTime: '',
        approveBy: '',
        approveTime: '',
        status: '',
        refuseReason: '',
        totalNum: 0
      }

      this.tableData = []

      this.detailId = ''
    },
    // 页面显示时触发
    open() {
      // 初始化表单数据
      this.initFormData()

      this.loadData()
    },
    async loadData() {
      this.loading = true
      await this.$api.sc.stock.transfer.scTransferOrder.get(this.id).then(res => {
        Object.assign(this.formData, {
          sourceScName: res.sourceScName,
          targetScName: res.targetScName,
          description: res.description,
          updateBy: res.updateBy,
          updateTime: res.updateTime,
          approveBy: res.approveBy,
          approveTime: res.approveTime,
          status: res.status,
          refuseReason: res.refuseReason,
          totalNum: res.totalNum,
          totalAmount: res.totalAmount
        })

        this.tableData = res.details
      }).finally(() => {
        this.loading = false
      })
    },
    detailReceive(detailId) {
      this.detailId = detailId
      this.$refs.detailReceiveDialog.openDialog()
    }
  }
}
</script>
<style>
</style>
